package com.example.arithmeticleetcode.leetcode.dec;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-23 09:41
 **/
public class Demo22 {

    public static void main(String[] args) {
        String[] votes = {"FVSHJIEMNGYPTQOURLWCZKAX","AITFQORCEHPVJMXGKSLNZWUY","OTERVXFZUMHNIYSCQAWGPKJL","VMSERIJYLZNWCPQTOKFUHAXG","VNHOZWKQCEFYPSGLAMXJIUTR","ANPHQIJMXCWOSKTYGULFVERZ","RFYUXJEWCKQOMGATHZVILNSP","SCPYUMQJTVEXKRNLIOWGHAFZ","VIKTSJCEYQGLOMPZWAHFXURN","SVJICLXKHQZTFWNPYRGMEUAO","JRCTHYKIGSXPOZLUQAVNEWFM","NGMSWJITREHFZVQCUKXYAPOL","WUXJOQKGNSYLHEZAFIPMRCVT","PKYQIOLXFCRGHZNAMJVUTWES","FERSGNMJVZXWAYLIKCPUQHTO","HPLRIUQMTSGYJVAXWNOCZEKF","JUVWPTEGCOFYSKXNRMHQALIZ","MWPIAZCNSLEYRTHFKQXUOVGJ","EZXLUNFVCMORSIWKTYHJAQPG","HRQNLTKJFIEGMCSXAZPYOVUW","LOHXVYGWRIJMCPSQENUAKTZF","XKUTWPRGHOAQFLVYMJSNEIZC","WTCRQMVKPHOSLGAXZUEFYNJI"};
        rankTeams(votes);
        String[] votes1 = {"ABC","ACB","ABC","ACB","ACB"};
        rankTeams2(votes1);

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(new Date());
    }
    public static String rankTeams(String[] votes) {
        if (votes == null) return null;
        if (votes.length == 1) return votes[0];
        Map<Character, Integer> map = new HashMap<>();
        for (String vote : votes) {
            for (int i = 0, len = vote.length(); i < len; i++) {
                char charAt = vote.charAt(i);
                map.put(charAt, map.getOrDefault(charAt, 0) + (i + 1) * (i + 1));
            }
        }
        if (map.size() == 1) {
            return String.valueOf(map.entrySet().iterator().next().getKey());
        }
        Map<Integer, List<Character>> countMap = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (countMap.containsKey(entry.getValue())) {
                List<Character> characters = countMap.get(entry.getValue());
                characters.add(entry.getKey());
                countMap.put(entry.getValue(), characters);
            } else {
                list.add(entry.getValue());
                List<Character> characters = new ArrayList<>();
                characters.add(entry.getKey());
                countMap.put(entry.getValue(), characters);
            }
        }
        Collections.sort(list);
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer integer : list) {
            List<Character> characters = countMap.get(integer);
            if (characters.size() < 2) {
                stringBuilder.append(characters.get(0));
            } else {
                Collections.sort(characters);
                for (Character character : characters) {
                    stringBuilder.append(character);
                }
            }
        }
        return stringBuilder.toString();
    }

    public static String rankTeams2(String[] votes) {
        //key是参赛团队，value是该团队每个排位获得的票数
        Map<Character, int[]> teamRankMap = new HashMap<>();

        for (String vote : votes) {
            for (int i = 0; i < vote.length(); i++) {
                int[] rankArr = teamRankMap.getOrDefault(vote.charAt(i), new int[26]);
                rankArr[i]++;
                teamRankMap.put(vote.charAt(i), rankArr);
            }
        }

        List<Map.Entry<Character, int[]>> teamRankList = new ArrayList<>(teamRankMap.entrySet());
        Collections.sort(teamRankList, (team1, team2) -> {
            int[] ranks1 = team1.getValue();
            int[] ranks2 = team2.getValue();
            //根据投票排序
            for (int i = 0; i < 26; i++) {
                if (ranks1[i] != ranks2[i]) {
                    return ranks2[i] - ranks1[i];
                }
            }
            //字母顺序排序
            return team1.getKey() - team2.getKey();
        });

        //转换为字符串输出
        return teamRankList.stream().map(entry -> String.valueOf(entry.getKey())).collect(Collectors.joining());
    }
}
